/// <reference path="../declare/dec.d.ts"/>

import * as handleChart from "./chartsInit.ts"
import {appData} from "./appData.ts"

let dontShowYAndM: string[] = ['Sex', 'Familycondition', 'Homeposition', 'Workposition', 'Hobbies']
let currentChartHandleFn: () => void
$('.side-bar').on('click', 'li', function (...args) {
    $('.side-bar li').removeClass('active');
    $(this).addClass('active');
    let link = $(this).data('link');
    $('.s-chart-c').hide();
    $(`#${link}`).parent().show();

    if (dontShowYAndM.indexOf(link) !== -1) {
        // console.log(link);
        $('.wrap-y,.wrap-m').hide()
    } else {
        $('.wrap-y,.wrap-m').show()
    }

    currentChartHandleFn = handleChart[`handle${link}Chart`];
    if (appData.initialize.indexOf(link) === -1) {
        appData.initialize.push(link);
        currentChartHandleFn()
    } else {
        console.warn('initialized')
    }
})

/*默认选中性别tab*/
setTimeout(() => {
    $('#side-sex').trigger('click')
}, 100)


$('.top select.province').on('change', function () {
    // Provice_id=$(this).val();
    // localStorage.setItem('d','d')
    appData.Province_id = Number($(this).val());
    appData.initialize.splice(0, appData.initialize.length)
})

/*年份选择*/
$('.top select.year').on('change', function () {
    appData.Year = Number($(this).val());
    appData.initialize.splice(0, appData.initialize.length)
})

/*月份过滤*/
let currentDate=new Date();
let month=currentDate.getMonth();
let monthStart=1;
while (monthStart<=month+1) {
    let option=document.createElement('option');
    $(option).text(monthStart).attr('value',monthStart);
    $('.month').append(option);
    monthStart++
}

/*月份选择*/
$('.top select.month').on('change', function () {
    appData.Month = Number($(this).val());
    appData.initialize.splice(0, appData.initialize.length)
})

$('#update-chart').on('click', function () {
    currentChartHandleFn && currentChartHandleFn()
})

/*初始化省份*/
$.ajax({
    url: 'http://m.sz404.com/api/getprovince',
    type: 'post',
    data: {
        sessionid: appData.Cookie
    },
    success(data) {
        if (data.code == 0) {
            for (let province of data.provinces) {
                $('#s-province').append(`<option value=${province.id}>${province.name}</option>`)
            }
            $('#s-province').val('2')
        }
    },
    error() {

    }
})

/**/
let user = localStorage.getItem('sgm_remember_username');
if (user) {
    $('#currentUser').text(user)
} else {
    location.assign('login.html')
}

$('input[name="sessionid"]').val(appData.Cookie)
$('#logout').on('click', function () {
    localStorage.removeItem('sgm_remember_username');
    localStorage.removeItem('sgm_remember_password');
    location.assign('login.html')
})


$('#reimport').on('click', function () {
    $('#already-upload').modal({
        show: true,
        backdrop: 'static'
    })
})


$('#upload-go').click(function () {
    var formdata = new FormData(<HTMLFormElement>document.getElementById('upload-form'));
    handleChart.upload(formdata,document.getElementById('already-upload'))
})



$(document.body).css('height', window.innerHeight).show()
// $('.side-bar').css('height',)


/**/